
package com.nifostasky.comic.config;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * 统一注解，解决前后端交互Long类型精度丢失的问题
 * 以及全局时间格式化,格式统一为: yy-MM-dd hh:mm:ss
 *
 */
@JsonComponent
public class JacksonConfig {
    @Value("yyyy-MM-dd HH:mm:ss")
    private String pattern;

    /**
     * 全局时间格式化配置
     * @return
     */
    @Bean("jackson2ObjectMapperBuilderCustomizer")
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(@Autowired LocalDateTimeSerializer localDateTimeSerializer) {
//        Jackson2ObjectMapperBuilderCustomizer customizer = new Jackson2ObjectMapperBuilderCustomizer() {
//            @Override
//            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
//                jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance)
//                        .serializerByType(Long.TYPE, ToStringSerializer.instance);
//            }
//        };
//        return customizer;
        return builder -> {
            //使用系统默认时区;
            TimeZone tz = TimeZone.getTimeZone(ZoneId.systemDefault());
            DateFormat df = new SimpleDateFormat(pattern);
            df.setTimeZone(tz);
            builder //解决前后端交互Long类型精度丢失的问题
                    .serializerByType(Long.class,ToStringSerializer.instance)
                    .serializerByType(Long.TYPE, ToStringSerializer.instance)
                    //LocalDateTime类型时间格式化
                    .serializerByType(LocalDateTime.class,localDateTimeSerializer)
                    //Date类型时间全局格式化
                    .failOnEmptyBeans(false)
                    .failOnUnknownProperties(false)
                    .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                    .dateFormat(df);
        };
    }

    /**
     * LocalDateTime类型,时间格式化配置
     */
    @Bean
    public LocalDateTimeSerializer localDateTimeDeserializer() {
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
    }
//
//    @Bean
//    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(@Autowired LocalDateTimeSerializer localDateTimeSerializer) {
//        return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeSerializer);
//    }
}